<?php

namespace App\Http\Controllers;

use App\Http\Requests\StorePostRequest;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PostController extends Controller
{


    public function __construct()
    {
        $this->middleware('auth')->except(['index','show']);
    }


    public function index()
    {
        $posts = Post::orderBy('created_at','desc')->paginate(10);
        return view('post.index',compact('posts'));
    }

    public function create()
    {
        return view("post.create");
    }

    public function store(StorePostRequest $request)
    {
      $data = $request->all();
      $data['user_id'] = Auth::id();
      $data['user_name'] = Auth::user()->name;
      $post = Post::create($data);
      if($post){
          return redirect()->route('post.index');
      }
    }


    public function show(Post $post)
    {
        return view('post.show',compact('post'));
    }

    public function edit(Post $post)
    {
        // 显示编辑帖子的表单
    }

    public function update(Request $request, Post $post)
    {
        // 更新指定帖子
    }

    public function destroy(Post $post)
    {
        // 删除指定帖子
    }
}
